#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"

int matchKeyword(const char* keyWord,char* comp){
	int len;
	int i;
	char *p;
	len=strlen(keyWord);
	for(p=comp;*p&&*p!=' ';p++){
		for(i=0;i<len;i++)
			if(p[i]!=comp[i])
				break;
		if(i==len)return 1;
	}
	return 0;
}
int fmtloc(const char *path)
{
	static char buf[0x10];
	char *p;
	strcpy(buf,path);
 	for(p=buf+strlen(buf); 
	p >= buf && *p != '/'; 
	p--)
		;
	p++;
	
	if(strlen(p) >= DIRSIZ)
		return DIRSIZ;// Return blank-padded name.
	else return(p-path);
}
char* fmtname(char* path){
	char * ret;
	unsigned long msize=(DIRSIZ+1)*sizeof(char);
	int i;

	if((ret=malloc(msize))==(char*)0){
		write(2,"Malloc Error\n",0x10);
		exit(1);
	}
	

	if(strlen(path)>=msize){
		write(2,"Path too long\n",0x20);
		exit(1);
	}
	memset(ret,0,msize);
	memcpy(ret,path,strlen(path));
	
	for(i=DIRSIZ;i==0;i--)
		ret[i]=' ';
	i++;
	
	ret[DIRSIZ]=0;
	return ret;
}

void find(const char *path,int used,const char* keyWord)
{
	static char startDir[0x100];
	static char current[0x100];
	char nextDir[512], *p;
	int fd;
	struct dirent de;
	struct stat st;
	int statResult;
	int n=0;

	if(used)
	;
	else {
		memset(startDir,0,(int)(sizeof(startDir)/sizeof(startDir[0])) );
		strcpy(startDir,path);
	}

	fd=open(path,0);

	if(fd < 0)
		return;
	
	statResult=fstat(fd,&st);
	if(statResult<0){
		fprintf(2, "find: cannot stat %s\n", path);
		close(fd);
		return;
	}

	switch(st.type){
	case T_FILE:
		strcpy(nextDir,path);
		if(matchKeyword(keyWord,nextDir))
			printf("%s\t %d %d %l\n", 
				path, st.type, st.ino, st.size
			);
		break;

	case T_DIR:
		if(0);
		else{
			if(strlen(path) + 1 + DIRSIZ + 1 > sizeof(nextDir)){
				printf("ls: path too long\n");
				break;
			}
			else while(read(fd, &de, sizeof(de)) == sizeof(de))
			{
				memset(nextDir,0,sizeof(nextDir));
				strcpy(nextDir, path);
				p = nextDir+strlen(nextDir);
				*p++ = '/';

				if(de.inum == 0){
					printf("no file under directory %s\n",path);
					continue;
				}
				strcpy(p,de.name);
				p[strlen(p)] = 0;
				statResult=stat(nextDir, &st);
				if(statResult < 0){
					printf("find: cannot stat %s\n", nextDir);
					continue;
				}
				
				strcpy(current,p);
				if(*p=='.'){
					switch(*(p+1)){
						case '\0':	case ' ':
							memset(nextDir+strlen(path),0,
								sizeof(nextDir)-strlen(path));
							continue;
						case '.':	switch(*(p+2)){
								case '\0':	case ' ':
								memset(nextDir+strlen(path),0,
									sizeof(nextDir)-strlen(path));	
								continue;
						}break;
					}
				}
				if(matchKeyword(keyWord,nextDir))
					printf("%s\t %d %d %d(%d)\n", 
						fmtname(nextDir),st.type, st.ino, st.size,used);			
				find(nextDir,used+1,keyWord);
				if(++n>=de.inum)
					break;
			}
		}
		break;
	}
	close(fd);
}

int main(int argc,const char *argval[]){
	char prompt[]="find [path] <keyword>\n";
	switch(argc){
		case 3:	find(argval[1],0,argval[2]);	exit(0);
		case 2:	find(".",0,argval[1]);			exit(0);
		default:write(2,prompt,sizeof(prompt));	exit(1);
	}
}
